﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// ChannelSalesByPlan needs a STATE col, a PROP_TRAC_NAME, TRAC_PROP_NAME, a GM col
/// </summary>
public class ChannelSalesByPlan
{
    private DateTime _ReportDate;

    public DateTime ReportDate
    {
        get
        {
            _ReportDate = ((DateTime)HttpContext.Current.Session["SalesDate"]).AddMonths(-Convert.ToInt32(((RollupMetadata)HttpContext.Current.Session["RollupMetadata"]).MonthsPrior));
            return _ReportDate;
        }
    }

    private DataView _dvChannelSalesByPlan;
    public ChannelSalesByPlan()
    {

        //_dvChannelSalesByPlan = ((System.Data.DataSet)HttpContext.Current.Cache["ACC_MARKET_PENETRATION"]).Tables[0].DefaultView;

        //if (!_dvChannelSalesByPlan.Table.Columns.Contains("PROPERTY"))
        //{
        //    _dvChannelSalesByPlan.Table.Columns.Add("PROPERTY");
        //}

        //if (((RollupMetadata)HttpContext.Current.Session["RollupMetadata"]).ByTracId)
        //{
        //    _dvChannelSalesByPlan.Sort = "TRAC_ID";
        //    _dvChannelSalesByPlan.Table.Columns["PROPERTY"].Expression = "TRIM(TRAC_ID) + ' - ' + PROPERTY_NM";
        //}
        //else
        //{
        //    _dvChannelSalesByPlan.Sort = "PROPERTY_NM";
        //    _dvChannelSalesByPlan.Table.Columns["PROPERTY"].Expression = "PROPERTY_NM +' (' + TRIM(TRAC_ID) + ')'";
        //}
    }

    public ChannelSalesByPlan(string psDud)
    {
    }

    public DataView Select(string psWhere)
    {
        string sTable = psWhere.Split('=')[0];

        _dvChannelSalesByPlan = ((System.Data.DataTable)HttpContext.Current.Cache["ACC_CHANNEL_ROLLUPS." + sTable]).DefaultView;
        DataView dv = _dvChannelSalesByPlan;
        //Add a datapoint for each day in the selected month, up to yesterday's date

        string DateFilter = String.Format(" AND MN='{0}' AND YR='{1}'", ReportDate.Month, ReportDate.Year);

        dv.RowFilter = psWhere + DateFilter;
        return dv;

    }

    private DataView Select(string psWhere, string psOrderBy)
    {
        DataView dv = Select(psWhere);
        dv.Sort = psOrderBy;
        return dv;
    }

    public DataView ByAreaMgr(string psAreaMgr)
    {
        return Select("AREA_MGR='" + psAreaMgr.Replace("'", "''") + "'");
    }

    public DataView ByAcctMgr(string psAcctMgr)
    {
        return Select("ACCT_MGR='" + psAcctMgr.Replace("'", "''") + "'");
    }

    public DataView ByMarket(string psMarket)
    {
        return Select("MARKET='" + psMarket + "'");
    }

    public DataView ByState(string psStateCD)
    {
        if (psStateCD == "ZZ")
            return Select("STATE_CD LIKE'%'");
        else
            //CREATE A STATE COL FROM TRAC ID OR USE SUBSTR?
            return Select("STATE_CD='" + psStateCD + "'");
    }

    public DataView ByPortfolio(string psPortfolio)
    {
        return Select("PORTFOLIO='" + psPortfolio.Replace("'", "''") + "'");
    }

    public DataView ByTRAC_ID(string psProperty)
    {
        return Select("TRAC_ID='" + psProperty.Replace("'", "''") + "'");
    }


    public DataView All()
    {
        return _dvChannelSalesByPlan;
    }

    public DataView ByRegion(string psRegion)
    {
        //JOIN GM TO YOUR GLOBAL DV
        return Select("REGION='" + psRegion.Replace("'", "''") + "'");
    }

    public DataView ByGenMgr(string psGenMgr)
    {
        //JOIN GM TO YOUR GLOBAL DV
        return Select("GEN_MGR='" + psGenMgr.Replace("'", "''") + "'");
    }


}